package server

import (
	"net/http"
)

const deploycontent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0" />
<title>UI版本管理</title>
<link
rel="stylesheet"
href="https://webtools.b-cdn.net/libs/element-ui.theme-chalk.css" />
<style>
*{padding:0;margin:0}html,body{width:100%;height:100%;overflow:hidden}.header{height:48px;line-height:48px;padding:0 15px;background-color:#001529;color:#fff;font-size:18px;font-weight:700}#app{width:100%;height:100%;padding:20px;background-color:#eee;box-sizing:border-box}.page{background-color:#fff;box-sizing:border-box;padding:15px}.search-form{padding:0 5px}.el-button--primary{background-color:#366efe !important}.el-pagination.is-background .el-pager li:not(.disabled).active{background-color:#366efe !important}
</style>
</head>
<body>
<header class="header">UI版本管理</header>
<div id="app">
<div class="page">
<div class="search-form">
<el-form inline>
<el-form-item>
<el-button
type="primary"
@click="dialogShow = true;">
上传版本
</el-button>
</el-form-item>
</el-form>
</div>
<el-table
:data="dataList"
:height="tableHeight">
<el-table-column
prop="Id"
label="编号"
width="75">
</el-table-column>
<el-table-column
prop="ModId"
label="上传人">
</el-table-column>
<el-table-column
prop="PathName"
label="所属平台">
</el-table-column>
<el-table-column
prop="FileData"
label="文件路径">
</el-table-column>
<el-table-column
prop="CreateTime"
label="创建时间">
<template slot-scope="scope">
<span>{{formatDate(scope.row, 'CreateTime')}}</span>
</template>
</el-table-column>
<el-table-column
fixed="right"
label="操作"
width="100">
<template slot-scope="scope">
<el-button
style="color: #366efe"
@click="handleRollback(scope.row.Id)"
type="text"
size="small">
回退到此版本
</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
style="margin-top: 15px; text-align: right"
background
layout="prev, pager, next"
:total="total"
@current-change="onChangePage">
</el-pagination>
<el-dialog
title="上传版本"
lock-scroll
modal-append-to-body
:visible.sync="dialogShow"
:before-close="beforeClose">
<el-form ref="refDialogForm">
<el-form-item>
<div
v-if="fileName && fileLink"
class="file-wrap"
@click="handleUpload">
<div class="file-item">文件名：{{ fileName }}</div>
</div>
<el-button
style="margin-bottom: 10px"
v-if="!fileName"
@click="handleUpload">
点击选择文件
</el-button>
</el-form-item>
<input
hidden
ref="uploadKeyword"
type="file"
accept=".zip"
@change="onUploadChange($event)" />
<el-form-item label="所属平台">
<el-select
clearable
v-model="pathName">
<el-option
v-for="item in pathList"
:key="item"
:value="item"
:label="item">
</el-option>
</el-select>
</el-form-item>
</el-form>
<div
class="dialog-footer"
slot="footer">
<el-button
type="primary"
@click="uploadVersion">
确认上传
</el-button>
</div>
</el-dialog>
</div>
</div>
<script src="https://webtools.b-cdn.net/libs/vue.min@2.6.10.js"></script>
<script src="https://webtools.b-cdn.net/libs/axios.min@0.18.1.js"></script>
<script src="https://webtools.b-cdn.net/libs/element-ui.min@2.15.14.js"></script>
<script>
function paramParse(e){return new URLSearchParams(e).toString()}Vue.prototype.$ELEMENT={size:"small",zIndex:3e3};const BASE_URI=location.protocol+"//"+location.host+"/api/",request=axios.create({baseURL:BASE_URI,timeout:15e3,headers:{"Content-Type":"application/x-www-form-urlencoded;charset=utf-8"}});request.interceptors.request.use(e=>(e.data="application/x-www-form-urlencoded;charset=utf-8"===e.headers["Content-Type"]?paramParse({...e.data}):e.data,e)),request.interceptors.response.use(e=>Promise.resolve(e||{}),e=>{const t=e.response||{};if("not login"!=t.data&&"账户已禁用"!=t.data)return Promise.resolve(t.data);window.location.href=BASE_URI+"login"});const app=new Vue({el:"#app",data:()=>({fileName:null,fileLink:null,total:0,pathName:"",tableHeight:"calc(100vh - 220px)",dataList:[],pathList:[],pager:{page_index:1,page_size:30},dialogShow:!1}),methods:{getVersionType(){request({url:"/adminMod/staticfile/query_search",method:"post"}).then(e=>{const t=e.data||{};this.pathList=t.PathNames||[]})},getVersionList(){request({url:"/adminMod/staticfile/query_list",method:"post",data:{...this.pager}}).then(e=>{const t=e.data||{};this.total=t.TotalCnt||0,this.dataList=t.List||[]})},uploadVersion(){if(!this.fileLink)return this.$message.error("文件尚未上传成功");const e=new FormData;e.append("file",this.fileLink,this.fileName),e.append("PathName",this.pathName),request({url:"/adminMod/staticfile/upload_file",method:"post",headers:{"Content-Type":"multipart/form-data;charset=utf-8"},data:e}).then(e=>{e.data,200==e.status&&this.$message.success("上传成功"),this.dialogShow=!1,this.getVersionList()})},handleRollback(e){this.$confirm("确定回退到此版本吗？","版本回退",{confirmButtonText:"确定",cancelButtonText:"取消",type:"warning"}).then(()=>{this.rollbackVersion(e)}).catch(()=>{})},rollbackVersion(e){request({url:"/adminMod/staticfile/reback_version",method:"post",data:{Id:e}}).then(e=>{200==e.status&&this.$message.success("回滚成功")})},beforeClose(e){this.fileName=null,this.fileLink=null,this.pathName="",this.$refs.uploadKeyword.value="",e()},handleUpload(){this.$refs.uploadKeyword.click()},onUploadChange(e){const t=e.target.files[0];this.fileLink=t,this.fileName=t.name,e.target.value=""},onChangePage(e){this.pager=e,this.getVersionList()},formatDate(e,t){let a=e[t],s=new Date(1e3*a),i=s.getMonth()+1,o=s.getDate();i<10&&(i="0"+(s.getMonth()+1)),o<10&&(o="0"+s.getDate());let n=s.getHours();n<10&&(n="0"+s.getHours());let r=s.getMinutes();r<10&&(r="0"+s.getMinutes());let l=s.getSeconds();return l<10&&(l="0"+s.getSeconds()),s.getFullYear()+"-"+i+"-"+o+" "+n+":"+r+":"+l}},mounted(){this.getVersionType(),this.getVersionList()}});
</script>
</body>
</html>
`

func handleDeploy(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Write([]byte(deploycontent))
}
